package com.easygoods.service;

import com.bestvike.stone.core.collection.CollectionUtils;
import com.bestvike.stone.core.lang.BeanUtils;
import com.bestvike.stone.core.lang.RandomUtils;
import com.bestvike.stone.spring.exception.BusinessException;
import com.bestvike.stone.spring.exception.ErrorCode;
import com.bestvike.stone.spring.service.BaseService;
import com.easygoods.dao.MemberHotSpuDao;
import com.easygoods.entity.MemberHotSpu;
import com.easygoods.entity.OrderGoods;
import com.easygoods.entity.OrderInfo;
import com.easygoods.entityex.GoodsSpuEx;
import com.easygoods.response.SpuListResponse;
import com.easygoods.shiro.auth.UserAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by 许崇雷 on 2019-03-08.
 */
@Service
public class HotSpuService extends BaseService {
    @Autowired
    private MemberHotSpuDao memberHotSpuDao;

    //过了退货时间后，增加热销商品的统计
    @Transactional
    public void increaseHotSpu(OrderInfo orderInfo, List<OrderGoods> orderGoodsList) {
        if (CollectionUtils.isEmpty(orderGoodsList))
            throw new BusinessException(ErrorCode.ERROR_SERVER, "该订单中不存在商品");

        for (OrderGoods orderGoods : orderGoodsList) {
            final Example example = new Example(MemberHotSpu.class);
            example.createCriteria().andEqualTo(MemberHotSpu.MEMBER_ID, orderInfo.getMemberId())
                    .andEqualTo(MemberHotSpu.SPU_ID, orderGoods.getSpuId());
            final MemberHotSpu memberHotSpu = this.memberHotSpuDao.selectOneByExample(example);
            if (memberHotSpu == null) {
                MemberHotSpu memberHotSpuAsInsert = new MemberHotSpu();
                memberHotSpuAsInsert.setId(RandomUtils.randomUUID());
                memberHotSpuAsInsert.setMemberId(orderInfo.getMemberId());
                memberHotSpuAsInsert.setSpuId(orderGoods.getSpuId());
                memberHotSpuAsInsert.setNum(orderGoods.getNum());
                memberHotSpuAsInsert.beforeInsert();
                int count = this.memberHotSpuDao.insert(memberHotSpuAsInsert);
                if (count != 1) {
                    this.log.error("入库商品热销记录表失败");
                    throw new BusinessException(ErrorCode.ERROR_SERVER, "热销商品添加失败");
                }
            } else {
                this.memberHotSpuDao.updateBySpuNum(memberHotSpu.getId(), orderGoods.getNum());
            }
        }
    }

    //获取该用户前5名热销商品
    public List<SpuListResponse> getHotSpuList() {
        final UserAuth userAuth = this.getAuth();
        if (userAuth == null)
            throw new BusinessException(ErrorCode.ERROR_SERVER, "无效的用户");
        final List<GoodsSpuEx> goodsByHotSpu = this.memberHotSpuDao.getHotSpuListByMemberId(userAuth.getMemberId());
        //类型转换
        return BeanUtils.toList(goodsByHotSpu, SpuListResponse.class);
    }
}
